package com.ucode.wap.hrm.controller;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.ucode.hrm.service.HrmJobPostApplyRecordService;
import com.ucode.hrm.service.HrmJobPostService;
import com.ucode.hrm.service.HrmPersonalResumeService;
import com.ucode.model.hrm.HrmJobPost;
import com.ucode.model.hrm.HrmJobPostApplyRecord;
import com.ucode.model.hrm.HrmPersonalResume;
import com.ucode.model.hrm.enums.HrmSysResultCodeEnum;
import com.ucode.tool.annotation.NeedLogin;
import com.ucode.tool.base.Paginator;
import com.ucode.tool.base.ResponseResult;
import com.ucode.tool.exception.UcodeServiceException;
import com.ucode.wap.base.core.CurrentLoginUserHandler;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Tag(name = "移动端 - 人力资源系统 - 招聘岗位")
@RestController
@RequestMapping("/hrm/job-post")
@Validated
public class ApiHrmJobPostController {

    @Resource
    private HrmJobPostService jobPostService;
    @Resource
    private HrmJobPostApplyRecordService jobPostApplyRecordService;
    @Resource
    private HrmPersonalResumeService personalResumeService;
    @Resource
    private CurrentLoginUserHandler currentLoginUserHandler;
    @GetMapping("/page-list")
    @Operation(summary = "获得分页列表")
    public ResponseResult<Paginator<HrmJobPost>> pageList(
            @Schema(description = "标题") @RequestParam(value = "title",required = false) String title,
            @Schema(description = "职位类型") @RequestParam(value = "jobType",required = false) Integer jobType,
            @Schema(description = "职位状态") @RequestParam(value = "status",required = false) Integer status,
            @Schema(description = "页码",example="1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @Schema(description = "每页记录数",example="5") @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){

        Map<String,Object> condition = new HashMap<>();
        if(StrUtil.isNotBlank(title))
            condition.put("title", StrUtil.concat(true,"%", title,"%"));

        condition.put("jobType",jobType);
        condition.put("status",status);
        condition.put("deltag",false);
        Paginator<HrmJobPost> paginator = jobPostService.queryPageForList(condition,pageSize,pageNum);
        return ResponseResult.success(paginator);
    }

    @GetMapping("/getById/{id}")
    @Parameter(description="主键ID",name = "id", required = true,in = ParameterIn.PATH,example = "1024")
    @Operation(summary = "主键获得信息")
    public ResponseResult<HrmJobPost> getById(@PathVariable(name="id",required=true) Long id){
        HrmJobPost bean = jobPostService.validate(id);
        return ResponseResult.success(bean);
    }

    @NeedLogin(validate = true)
    @PostMapping("/deliver/{id}")
    @Parameter(description="主键ID",name = "id", required = true,in = ParameterIn.PATH,example = "1024")
    @Operation(summary = "投递简历")
    public ResponseResult<HrmJobPost> deliver(@PathVariable(name="id",required=true) Long id){

        HrmPersonalResume personalResume = personalResumeService.selectOneByMemberId(currentLoginUserHandler.getCurrentUserId());
        if(ObjUtil.isNull(personalResume)){
            throw new UcodeServiceException(HrmSysResultCodeEnum.PERSONAL_RESUME_NOT_COMPLETE);
        }

        HrmJobPostApplyRecord jobPostApplyRecord = new HrmJobPostApplyRecord();
        jobPostApplyRecord.setJobPostId( id);
        jobPostApplyRecord.setResumeId(personalResume.getId());
        jobPostApplyRecord.setMemberId(personalResume.getMemberId());
        jobPostApplyRecordService.insert(jobPostApplyRecord);
        return ResponseResult.success();
    }


    @GetMapping("/deliver-list")
    @Operation(summary = "获得投递记录分页列表")
    public ResponseResult<Paginator<HrmJobPost>> pageDeliverList(
            @Schema(description = "页码",example="1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @Schema(description = "每页记录数",example="5") @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){

        Map<String,Object> condition = new HashMap<>();
        //if(StrUtil.isNotBlank(title))
        //    condition.put("title", StrUtil.concat(true,"%", title,"%"));
        //
        //condition.put("jobType",jobType);
        //condition.put("status",status);
        condition.put("deltag",false);
        Paginator<HrmJobPost> paginator = jobPostService.queryPageForList(condition,pageSize,pageNum);
        return ResponseResult.success(paginator);
    }

}
